package com.shareach.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BaseBrandDao;
import com.shareach.domain.BaseBrandDomain;
import com.shareach.service.BaseBrandService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.ReflectionUtil;

/**
 * create time：2013-1-30 下午6:02:20
 * 
 * @author retryu
 * @since JDK 1.6
 * @version 1.0 description：
 */
@Service
public class BaseBrandServiceImpl extends BaseService<BaseBrandDomain> implements BaseBrandService {

	@Autowired
	BaseBrandDao baseBrandDao;

	@Override
	public synchronized Long save(BaseBrandDomain domain) {
		domain.setCreatTime(new Date());
		return baseBrandDao.insert(domain);
	}

	@Override
	public void del(BaseBrandDomain domain) {
		if (domain.getId() == null)
			return;
		Query query = new Query();
		query.addCriteria(new Criteria("_id").is(domain.getId()));
		baseBrandDao.delete(query, BaseBrandDomain.class);
	}

	@Override
	public synchronized void update(BaseBrandDomain domain) {
		if (domain.getId() == null)
			return;
		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() > 0) {
			baseBrandDao.update(Criteria.where("_id").is(domain.getId()), update);
		}
	}

	@Override
	public BaseBrandDomain getById(Long id) {
		if (id == null)
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BaseBrandDomain baseBrandDomain = baseBrandDao.findOne(query);
		return baseBrandDomain;
	}

	@Override
	public List<BaseBrandDomain> getList(BaseBrandDomain domain) {
		Query query = parseDomaintoQuery(domain);

		List<BaseBrandDomain> baseBrandDomains = baseBrandDao.findList(query);
		return baseBrandDomains;
	}

	@Override
	public Page<BaseBrandDomain> getPageResult(BaseBrandDomain domain, BaseQueryOrder order) {
		Map<Object, Object> domainMap = ReflectionUtil.parseDomaintoMap(domain);
		Query query = new Query();
		for (Entry<Object, Object> entry : domainMap.entrySet()) {
			if (entry.getKey().equals("name")) {
				query.addCriteria(Criteria.where((String) entry.getKey()).regex((String) entry.getValue()));
			} else if (entry.getKey().equals("updateTime")) {
				continue;
			} else {
				query.addCriteria(Criteria.where((String) entry.getKey()).is(entry.getValue()));
			}
		}
		if (null != domain.getId() && !domainMap.containsKey(domain.getId())) {
			query.addCriteria(Criteria.where("_id").is(domain.getId()));
		}
		Page<BaseBrandDomain> domainPage = baseBrandDao.getPageResult(query, order);
		return domainPage;
	}

	@Override
	public BaseBrandDomain findOne(BaseBrandDomain domain) {
		Query query = parseDomaintoQuery(domain);
		return baseBrandDao.findOne(query);
	}

	@Override
	public synchronized void update(BaseBrandDomain domain, BaseBrandDomain condition) {
		Query query = parseDomaintoQuery(condition);

		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() != 0) {
			baseBrandDao.update(query, update);
		}
	}

}
